#include "fann.h"

//char *addneuron()
struct inlayer{
int lr;
int inlr;
};

struct inlayer whichlayer(struct fann *ann, struct fann_neuron *nr)
{
	int i = 0;
	struct inlayer result;
	result.lr=0; result.inlr=0;
	for(struct fann_layer *l = ann->first_layer; l<=ann->last_layer; l++)
	{
		if(nr>=l->first_neuron && nr<=l->last_neuron){
			result.lr = i;
			result.inlr = nr - l->first_neuron;
			return result;
		}
		i++;
	}
	return result;
}

void svgwrite(struct fann *ann)
{	
	FILE *fplot = fopen("svgout.svg", "w");
	char *tstro = "thesets";
	char **clrs[9];
	clrs[0] = "33,127,127";
	clrs[1] = "33,127,255";
	clrs[2] = "127,33,127";
	clrs[3] = "33,127,33";
	clrs[4] = "255,33,127";
	clrs[5] = "127,33,255";
	clrs[6] = "33,255,255";
	clrs[7] = "127,127,33";
	clrs[9] = "255,127,33";
	int il = 0, jl, in = 0, i, neuron, ic;
	struct fann_neuron **neuron_pointers;
	struct inlayer *lns = calloc(ann->total_neurons, sizeof(struct inlayer));
	struct fann_neuron *neurons = ann->first_layer->first_neuron;
	const int rd = 5+ann->last_layer-ann->first_layer;
	
	for(i = 0; i<ann->total_neurons; i++)
	{
		lns[i] = whichlayer(ann, &neurons[i]);
	}
	fprintf(fplot, "<svg>\n");
	for(struct fann_layer *l = ann->first_layer; l<ann->last_layer; l++)
	{
		for(struct fann_neuron *n = l->first_neuron; n<l->last_neuron; n++)
		{
		fprintf(fplot, "<circle cx=\"%d\" cy=\"%d\" r=\"%d\" fill=\"none\" stroke=\"rgb(%s)\" stroke-width=\"1\"/>\n", rd+il*rd*3, rd+in*rd*2, rd, clrs[il]);
		in++;
		}
		il++;in=0;
	}
	il=0;
	for(struct fann_layer *l = ann->first_layer; l<ann->last_layer; l++)
        {
		printf("%d Enter\n", il);
                for(struct fann_neuron *n = l->first_neuron; n<l->last_neuron; n++)
                {
			for(int con=n->first_con; con<=n->last_con; con++)
			{
				neuron = ann->connections[con] - ann->first_layer->first_neuron;
				//printf("con = %d nc = %d|", con, ic);
			        //printf("neuron = %d lns[neuron].inlr = %d \n", neuron, lns[neuron].inlr);
				jl = lns[neuron].inlr;
                		fprintf(fplot, "<circle cx=\"%d\" cy=\"%d\" r=\"%d\" fill=\"none\" stroke=\"rgb(%s)\" stroke-width=\"1\"/>\n", rd+lns[neuron].lr*rd*3, rd+jl*rd*2, 4+il, clrs[il]);
                		ic++;
			}
			ic=0;
                }
		printf("%d Ok\n", il);
                il++;in=0;
        }

	fprintf(fplot, "</svg>");
	fclose(fplot);
}

